package com.felix.mongo;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

/**
 * DBtransfer
 *
 * @author xjh
 * @version 1.0
 * @date 2019-03-21
 */
public class DBtransfer {
    public static void main(String[] args) {
        //数据库地址
        MongoClient client = new MongoClient("10.27.21.148", 27017);
        //数据库名
        MongoDatabase database = client.getDatabase("fc_v7_test");
        //要转移数据的表名
        MongoCollection<Document> collection = database.getCollection("turbine");
        //数据库名
        MongoDatabase database2 = client.getDatabase("power_curve");
        //放入的表名
        MongoCollection<Document> collection2 = database2.getCollection("turbine");
        //iterator——迭代
        FindIterable<Document> findIterable = collection.find();
        //游标
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            Document d = mongoCursor.next(); //遍历每一条数据
            collection2.insertOne(d);
        }
        System.out.println("转移成功");
    }

}
